package com.text.mqtt_text.service.impl;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.text.mqtt_text.service.AIService;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class AIServerImpl implements AIService {
      public GenerationResult callWithMessage(String status) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("你现在是一个情感分析大师，用户会给你发送一段话，是关于孩子和AI的对话总结，请你根据文本生成1到10个不等的数值，，数值的大小是1到100，用来反映孩子的情绪变化。注意：你只能回答数值，数值之间使用逗号隔开，其他的内容都不要输出。")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(status)
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量，请用阿里云百炼API Key将下行替换为：.apiKey("sk-xxx")
                .apiKey("sk-760961e7842c480997d16f36c559acac")
                // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
}
